package GUI;

import Enumeration.ChoosenDate;
import Enumeration.GUIButtons;
import Facade.Facade;
import Parser.ACreatorParser;
import com.toedter.calendar.JDateChooser;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * @author Martin
 * @version 1.0
 * @created 14-III-2012 13:12:30
 */
public class GUI extends Subject {

    private Color BARVA_OKNO = new Color(135, 206, 235, 45);
    // k dispozici pro upravu operaci
    private JPanel[] datumyMoznosti = new JPanel[3];
    private JPanel left;
    private JTextField[] vysledky;

    public GUI(Facade facade) {
        super(facade);
        datumy[0] = new JDateChooser();
        datumy[1] = new JDateChooser();
        panelParser();
        initOperationPanel();
        initResultPanel();
        inicializace();
    }

    private void inicializace() {
        okno = new JFrame("Program-STI");
        okno.setSize(500, 300);
        okno.setLocation(0, 0);
        okno.setPreferredSize(new Dimension(500, 300));
        okno.setMinimumSize(new Dimension(400, 250));
        okno.setMaximumSize(new Dimension(900, 700));
        okno.setVisible(false);
        okno.setLayout(new GridBagLayout());

        okno.addWindowListener(new WindowAdapter() { //pridame listenera na okno->musime az po inicializaci menu, protoze vyuziva jeho metody

            @Override
            public void windowClosing(WindowEvent e) {
                //okno.dispose();
                konec();
            }
        });

        okno.setFocusable(true);
        //podminky pro layout
        GridBagConstraints c = new GridBagConstraints();

        //hlavni panel
        c.insets = new Insets(10, 10, 10, 10);  //top padding
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        pozadi = new JPanel(new GridBagLayout());
        pozadi.setBackground(Color.WHITE);
        okno.add(pozadi, c);
        c.weighty=0;
        pozadi.add(hlavniPanel,c);  
        //hlavniPanel.setBackground(BARVA_OKNO);
        hlavniPanel.add(operationFrame, c);  //vystuvni frame
        c.gridy=1;
        hlavniPanel.add(parserPanel,c);
        okno.setVisible(true);
    }

    private void initOperationPanel() {
        JPanel vybery = new JPanel(new GridBagLayout());
        vybery.setBackground(BARVA_OKNO);
        JLabel textLabel = new JLabel("Možnosti zadání datumů:");
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 2, 2);  //top padding
        c.fill = GridBagConstraints.BOTH;
        c.ipady = 8;
        c.weightx = 0;
        c.ipadx = 30;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.gridx = 0;
        c.gridy = 0;
        vybery.add(textLabel, c);

        initDatumyMoznosti();

        ButtonGroup group = new ButtonGroup();
        JRadioButton moznost;
        for (ChoosenDate choosen:ChoosenDate.values()) {
            moznost = new JRadioButton(choosen.getType());
            moznost.setActionCommand(choosen.getType());
            //System.out.println(String.valueOf(c.gridy));
            //moznost.setBackground(BARVA_OKNO); //!!!!! tohle alesovi hazi divne stiny
            moznost.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    GridBagConstraints c = new GridBagConstraints();
                    c.fill = GridBagConstraints.BOTH;
                    c.anchor=GridBagConstraints.FIRST_LINE_START;
                    c.weightx = 1;
                    c.weighty = 1;
                    c.gridy = 0;
                    c.gridx = 0;
                    c.ipadx=10;
                    try {
                        left.setVisible(false);
                        vybranaMoznost = vybranaMoznost.getChoosen(e.getActionCommand());
                        left.removeAll();
                        
                        left.add(datumyMoznosti[vybranaMoznost.getIndex()], c);
                        left.revalidate();
                        left.setVisible(true);
                    } catch (Exception ex) {
                        System.out.println(ex.getMessage());
                    }
                }
            });
            c.gridy++;
            group.add(moznost);
            vybery.add(moznost, c);
        }
        group.setSelected(group.getElements().nextElement().getModel(), true);
        c.ipadx=10;
        c.ipady=0;
        c.gridy = 0;
        c.weightx=0;
        operationFrame.add(vybery, c);
        left = new JPanel(new GridBagLayout());
        c.weightx=1;
        c.weighty=1;
        left.add(datumyMoznosti[vybranaMoznost.getIndex()], c);
        c.gridx = 1;       
        operationFrame.add(left, c); //prvotni nastaveni
        c.gridy=1;
        c.gridx=0;       
        c.gridwidth=2;
        JButton tlacitkoVypocet = new JButton(GUIButtons.vypocet.getText());
        tlacitkoVypocet.setName(GUIButtons.vypocet.getType());
        tlacitkoVypocet.addActionListener(this);
        operationFrame.add(tlacitkoVypocet,c);       
    }

    private void initDatumyMoznosti() {
        //prvni od do
        datumyMoznosti[0] = new JPanel(new GridBagLayout());
        datumyMoznosti[0].setBackground(BARVA_OKNO);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 2, 2);  //top padding
        c.fill = GridBagConstraints.BOTH;
        c.ipady = 8;
        c.weightx = 1;
        c.ipadx = 40;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        datumyMoznosti[0].add(new Label("Výběr datumu od-do:"), c); //popis
        c.gridy = 1;
        c.gridwidth = 1;
        datumyMoznosti[0].add(new Label("Od:"), c); //popis   
        c.gridx = 1;
        datumyMoznosti[0].add(datumy[0], c);
        c.gridy = 2;
        c.gridx = 0;
        datumyMoznosti[0].add(new Label("Do:"), c); //popis 
        c.gridx = 1;
        datumyMoznosti[0].add(datumy[1], c);
        //druhy
        datumyMoznosti[1] = new JPanel(new GridBagLayout());
        datumyMoznosti[1].setBackground(BARVA_OKNO);
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        datumyMoznosti[1].add(new Label("Výběr měsíce:"), c); //popis
        c.gridy = 1;
        c.gridwidth = 1;
        datumyMoznosti[1].add(new Label("Měsíc:"), c); //popis   
        c.gridx = 1;
        datumyMoznosti[1].add(mesic, c);
        c.gridy = 2;
        c.gridx = 0;
        datumyMoznosti[1].add(new Label("Rok:"), c); //popis 
        c.gridx = 1;
        datumyMoznosti[1].add(rok, c);
        //treti
        datumyMoznosti[2] = new JPanel(new GridBagLayout());
        datumyMoznosti[2].setBackground(BARVA_OKNO);
        c.gridx = 0;
        c.gridy = 0;
        datumyMoznosti[2].add(new Label("Jsou vybrány všechny data."), c); //popis       
    }

    private void initResultPanel() {
        JPanel vypocty = new JPanel(new GridBagLayout());
        vypocty.setBackground(BARVA_OKNO);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(2, 2, 2, 2);  //top padding
        c.fill = GridBagConstraints.BOTH;
        c.ipady = 8;
        c.ipadx = 40;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx=1;
        //c.weighty=1;
        vypocty.add(new JLabel("Max průtok"),c);
        c.gridx=1;
        vypocty.add(new JLabel("Min průtok"),c);
        c.gridx=2;
        vypocty.add(new JLabel("Průměrný průtok"),c);
        c.gridx=0;
        c.gridy=1;
        vysledky = new JTextField[3];
        vysledky[0] = new JTextField();
        vysledky[1] = new JTextField();
        vysledky[2] = new JTextField();
        update();
        vypocty.add(vysledky[0],c);
        c.gridx=1;
        vypocty.add(vysledky[1],c);
        c.gridx=2;
        vypocty.add(vysledky[2],c);
        c.gridx=0;
        c.gridy=0;
        c.gridwidth=2;
        resultFrame.add(vypocty,c);
        c.gridwidth=1;
        c.gridy=1;  
        c.weightx=0.5;
        c.ipadx=0;
        JButton tlacitkoExport = new JButton(GUIButtons.export.getText());
        tlacitkoExport.setName(GUIButtons.export.getType());
        tlacitkoExport.addActionListener(this);
        resultFrame.add(tlacitkoExport,c); 
        c.gridx=1;
        JButton tlacitkoZpet = new JButton(GUIButtons.zpet.getText());
        tlacitkoZpet.setName(GUIButtons.zpet.getType());
        tlacitkoZpet.addActionListener(this);
        resultFrame.add(tlacitkoZpet,c);               
    }
    
    private void panelParser() {
        GridBagConstraints c = new GridBagConstraints();
        parserPanel = new JPanel(new GridBagLayout());
        //parserPanel.setBackground(BARVA_OKNO);
        c.gridx=0;        
        c.gridy=0;
        c.weightx=0;
        c.fill = GridBagConstraints.BOTH;
        parserCinnost = new JLabel("Aktivuji parser");        
        parserPanel.add(new JLabel("Činnost parseru: "),c);
        c.gridx=1;
        parserPanel.add(parserCinnost,c);
    }
    
    //ziska si data sama z resultu
    @Override
    public void update() {
        if (vysledky!=null) {
            vysledky[0].setText(String.valueOf(resultData.getMax()));
            vysledky[1].setText(String.valueOf(resultData.getMin()));
            vysledky[2].setText(String.valueOf(resultData.getAverage()));
        } else {
            messageOperation("Nepodařilo se nastavit data");
        }
    }
}